<?php
defined('_JEXEC') or die('Restricted access');
require_once(JPATH_COMPONENT.DS.'persistencia'.DS.'DAOEvent.php');
require_once('FTConnection.php');

class FTDAOEvent implements DAOEvent{
	
	private $_connection;
	private $_ftId = '630280';
	
	public function __construct(){
		$this->_connection=FTConnection::getInstance();
	}
	
	private function getEventRowId($id){
		$row=$this->_connection->query("SELECT ROWID FROM ".$this->_ftId." WHERE idEvento=".$id);
		return $row[0]['rowid'];
	}
	
	public function saveEvent($event) {
		//Viene en formato YYYY-MM-DD y queremos MM-DD-YYYY
		$dateArray=explode("-", $event->getDate());
		$date=$dateArray[1]."/".$dateArray[2]."/".$dateArray[0];
		$path="INSERT INTO " . $this->_ftId . " (idEvento,idCreador,idCategoria,idAreaAdministrativa,estado,nombre,descripcion,imagen,coordenada,fecha,numVisitas,hora) VALUES (" . $event->getIdEvent () . ",'" . $event->getNameCreator () . "','" . $event->getNameCategory () . "','" . $event->getAreaAdmin ()->getNameArea() . "','" . $event->getStatus (). "','". $event->getNameEvent () . "','" . $event->getDescription () . "','" . $event->getImage () . "','".$event->getCoord()."','" . $date . "'," . $event->getContadorVisitas () . ",'" . $event->getHora ()."')" ;
		return $this->_connection->query ( $path );
	}

public function updateEvent($event){
		$eventROWID=$this->getEventRowId($event->getIdEvent());
		
		//$newNumVisitas=$this->getEventViews($event->getIdEvent());
		
		$newNameCreator=$event->getNameCreator();
		$newNameCategory=$event->getNameCategory();
		$newNameAreaAdmin=$event->getAreaAdmin()->getNameArea();
		$newCategory=$event->getCategory();
		$newIdCreator=$event->getIdCreator();
		$newIdEvent=$event->getIdEvent();
		$newNameEvent=$event->getNameEvent();
		$newDescription=$event->getDescription();
		$newImage=$event->getImage();
		$newDate=$event->getDate();
		$newHora=$event->getHora();
		$newCoord=$event->getCoord();
		$newContadorVisitas=$event->getContadorVisitas();
		$newStatus=$event->getStatus();
		
		$ok=$this->_connection->query("UPDATE ".$this->_ftId.
								  " SET numVisitas=".$newContadorVisitas.",
								  idEvento=".$newIdEvent.",
								  idCreador='".$newNameCreator."',
								  idCategoria='".$newNameCategory."',
								  idAreaAdministrativa='".$newNameAreaAdmin."',
								  estado='".$newStatus."',
								  nombre='".$newNameEvent."',
								  descripcion='".$newDescription."',
								  imagen='".$newImage."',
								  coordenada='".$newCoord."',
								  fecha='".$newDate."',
								  hora='".$newHora.
								  "' WHERE ROWID='".$eventROWID."'");
		
		return true;
	}
	public function readEvents(){

	}
	
	public function readEventById($idEvent){

	}
	
	public function getEventViews($idEvent){
		$row=$this->_connection->query("SELECT numVisitas FROM ".$this->_ftId." WHERE idEvento=".$idEvent);
		return $row[0]['numVisitas'];
	}
	
	public function addEventView($idEvent){
		$eventROWID=$this->getEventRowId($idEvent);
		$newNumVisitas=$this->getEventViews($idEvent)+1;
		$ok=$this->_connection->query("UPDATE ".$this->_ftId.
								  " SET numVisitas=".$newNumVisitas.
								  " WHERE ROWID='".$eventROWID."'");
		return true;
	}
	
	public function deleteEvent($idEvent){
		//print_r("el id que estoy buscando es :::: ".$idEvent);
		$eventROWID=$this->getEventRowId($idEvent);
		$ok=$this->_connection->query("DELETE FROM ".$this->_ftId." WHERE ROWID='".$eventROWID."'");
	}
}

?>